home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / PARSE1.C < prev    next >
C/C++ Source or Header  |  1993-06-10  |  3KB  |  97 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 06-06-93 (18:37)             Number: 223
  4. From: PETER JACKSON                Refer#: 198
  5.   To: ROLLIN WHITE                  Recvd: NO  
  6. Subj: Parsing a Text file            Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8.  -=> Quoting Rollin White to All <=-
  9.  
  10.  RW> Does anyone have code(or even a reccommended
  11.  RW> approach) fo reading in a text file that will serve as a
  12.  RW> program's configuration file?  Specifically, keywords like:
  13.  
  14.  RW> USER=John Doe
  15.  
  16.  RW> Any ideas are appreciated!
  17.  
  18. Here's one idea. It started out as a quick thing centered
  19. around strtok(), but it, as usual, proved a little more
  20. complex to write when trying to handle multi word values
  21. ie. "John Doe". Anyway, this is tested and works.
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. #ifndef TRUE
  27. #define TRUE 1
  28. #define FALSE 0
  29. #endif
  30.  
  31. void main()
  32. {
  33.     char    option[30],     /* arbitrary length */
  34.             value[30],      /* arbitrary length */
  35.             buf[60],        /* arbitrary length */
  36.             first,          /* flags multiple words for value */
  37.             *ptr;
  38.     FILE    *fpi;
  39.  
  40.     if( (fpi=fopen("MYFILE.CFG", "r")) == (FILE *)NULL )
  41.     {
  42.         perror("Could not open MYFILE.CFG");
  43.         return;
  44.     }
  45.  
  46.     memset(option, '\0', sizeof(option));
  47.     memset(buf, '\0', sizeof(buf));
  48.     ptr=(char *)NULL;
  49.  
  50.     while( fgets(buf, sizeof(buf), fpi) )       /* read one full line */
  51.     {
  52.         memset(value, '\0', sizeof(value));
  53.         first=TRUE;
  54.         buf[strlen(buf)-1]='\0';           /* change \n to \0 */
  55.         strcpy(option, strtok(buf, "= \t"));
  56.  
  57.         /* this block handles 'multi-word' values */
  58.         while( ptr=strtok((char *)NULL, "= \t") )   /* look for word */
  59.         {
  60.             if( !first )        /* seperate words in value */
  61.                 strcat(value, " ");
  62.             first=FALSE;
  63.             strcat(value, ptr);
  64.         }
  65.         /* ... process option ... */
  66. printf("Option: [%s]\tValue: [%s]\n", option, value);   /* debug */
  67.     }
  68.  
  69.     fclose(fpi);
  70. }
  71.  
  72. /* Here is a sample config file that tests various input possibilities */
  73.  
  74. #ifdef CompileTestDataAndBlowUp
  75. SETUP=setup
  76. OTHER = other
  77. SOME= some
  78. WHAT =what
  79.  
  80. BLANK=did you get that?
  81. TAB =   tab
  82. #endif
  83.  
  84.  
  85. Hope it's of some use.
  86.  
  87. -pbj-
  88.  
  89. ... Post no bills
  90. ___ Blue Wave/QWK v2.12
  91.  
  92. --- Maximus/2 2.01wb
  93.  * Origin: Treasure Island =HST/DS= 203-791-8532 (1:141/730)
  94. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  95. SEEN-BY: 153/752 154/40 77 157/110 159/100 125 430 575 950 203/23 209/209
  96. SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
  97.